package cn.tedu.crm.staff.service.impl;

import cn.tedu.crm.operatelog.loggable.Loggable;
import cn.tedu.crm.operatelog.loggable.OperationModule;
import cn.tedu.crm.operatelog.loggable.OperationType;
import cn.tedu.crm.staff.mapper.StaffMapper;
import cn.tedu.crm.staff.pojo.dto.StaffParam;
import cn.tedu.crm.staff.pojo.dto.StaffQuery;
import cn.tedu.crm.staff.pojo.entity.Staff;
import cn.tedu.crm.staff.pojo.vo.StaffVO;
import cn.tedu.crm.staff.service.StaffService;
import cn.tedu.crm.user.mapper.UserMapper;
import cn.tedu.crm.user.pojo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class StaffServiceImpl implements StaffService{
    @Autowired
    StaffMapper staffMapper;

    @Autowired
    UserMapper userMapper;


    @Override
    public List<StaffVO> selectStaff(StaffQuery query) {
        log.debug("查询员工业务:{}",query);
        return staffMapper.selectStaff(query);
    }


    @Override
    public StaffVO selectById(Long id) {
        log.debug("根据id查询部门信息业务:{}", id);
        StaffVO staff = staffMapper.selectById(id);
        if(staff != null){
            StaffVO staffVO = new StaffVO();
            BeanUtils.copyProperties(staff,staffVO);
            return staffVO;
        }
        return null;
    }


    @Override
    @Loggable(module = OperationModule.EMPLOYEE_MANAGEMENT,type = OperationType.INSERT_UPDATE)
    public void addStaff(StaffParam staffParam) {
        log.debug("添加员工业务:{}",staffParam);
        Staff staff = new Staff();
        BeanUtils.copyProperties(staffParam,staff);
        User user = new User();
        if (staff.getId() == null){
            staff.setCreateTime(new Date());
            staffMapper.insert(staff);
        }else {
            staff.setUpdateTime(new Date());
            staffMapper.updateById(staff);
        }
        user.setId(staff.getUserId());
        user.setPhone(staffParam.getPhone());
        userMapper.updateById(user);
    }

    @Override
    @Loggable(module = OperationModule.EMPLOYEE_MANAGEMENT,type = OperationType.DELETE)
    public void deleteStaff(Long id) {
        log.debug("删除员工业务:{}", id);
        staffMapper.deleteById(id);
    }
}




